/**
 * This script contains tests for the FlyTED Sparql Endpoint.
 * @author Alistair Miles
 * @author Jun Zhao
 */
// convenience variables
var Assert = YAHOO.util.Assert;
var log = YAHOO.log;
var JSON = YAHOO.lang.JSON;
var sparqlns = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> ";
sparqlns += "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> ";
sparqlns += "PREFIX dro: <http://imageweb.zoo.ox.ac.uk/drosophila#> ";
sparqlns += "PREFIX flybase: <http://rodos.zoo.ox.ac.uk/2008/flyweb/ontologies/flybase/>";

/**
 * @class
 */
function tests() {}

function testEndpointExists(testcase) {

	// define the callback object
	var callback = {
		success: testEndpointExistsResponseSuccess(testcase),
		failure: testEndpointExistsResponseFailure(testcase)
	};
	
	log("Make the request");
	var request = YAHOO.util.Connect.asyncRequest("GET", "flybase", callback, null);
	
	log("Suspend the test runner");
	testcase.wait();
	
	log("N.B. this code should never be reached.");
}

tests.testEndpointExists = testEndpointExists;

function testEndpointHasGenes (testcase) {
	var query = "ASK { ?gene rdf:type flybase:Gene }";
	testAskQuery("flybase", testcase, query, true, "Endpoint has no genes.");
}

tests.testEndpointHasGenes = testEndpointHasGenes;

function testEndpointHasGenesWithFlyBaseId (testcase) {
	var query = "ASK { ?gene flybase:flybaseID ?flybaseId }";
	testAskQuery("flybase", testcase, query, true, "Endpoint has no genes with flybaseID property.");
}

tests.testEndpointHasGenesWithFlyBaseId = testEndpointHasGenesWithFlyBaseId;

function testEndpointHasNoGenesWithoutFlyBaseId (testcase) {
	var query = "ASK { ?gene rdf:type flybase:Gene . OPTIONAL {?gene flybase:flybaseID ?flybaseId }. FILTER ( !bound(?flybaseId)) }";
	testAskQuery("flybase", testcase, query, false, "Expected no genes without flybase ids.");	
}

tests.testEndpointHasNoGenesWithoutFlyBaseId = testEndpointHasNoGenesWithoutFlyBaseId;

function testEndpointHasNoGenesWithMultipleFlyBaseId (testcase) {
	var query = "ASK { ?gene flybase:flybaseID ?flybaseId1, ?flybaseId2 . FILTER ( ?flybaseId1 != ?flybaseId2 ) }";
	testAskQuery("flybase", testcase, query, false, "Expected no genes with multiple flybase ids.");	
}

tests.testEndpointHasNoGenesWithMultipleFlyBaseId = testEndpointHasNoGenesWithMultipleFlyBaseId;

function testEndpointHasGenesWithSymbol (testcase) {
	var query = "ASK { ?gene flybase:symbol ?symbol }";
	testAskQuery("flybase", testcase, query, true, "Endpoint has no genes with symbol property.");
}

tests.testEndpointHasGenesWithSymbol = testEndpointHasGenesWithSymbol;

function testEndpointHasNoGenesWithoutSymbol (testcase) {
	var query = "ASK { ?gene rdf:type flybase:Gene . OPTIONAL {?gene flybase:symbol ?symbol }. FILTER ( !bound(?symbol)) }";
	testAskQuery("flybase", testcase, query, false, "Expected no genes without symbols.");	
}

tests.testEndpointHasNoGenesWithoutSymbol = testEndpointHasNoGenesWithoutSymbol;

function testEndpointHasNoGenesWithMultipleSymbol (testcase) {
	var query = "ASK { ?gene flybase:symbol ?symbol1, ?symbol2 . FILTER ( ?symbol1 != ?symbol2 ) }";
	testAskQuery("flybase", testcase, query, false, "Expected no genes with multiple symbols.");	
}

tests.testEndpointHasNoGenesWithMultipleSymbol = testEndpointHasNoGenesWithMultipleSymbol;

function testEndpointHasGenesWithAnnotationSymbol (testcase) {
	var query = "ASK { ?gene flybase:annotationSymbol ?annotationSymbol }";
	testAskQuery("flybase", testcase, query, true, "Endpoint has no genes with annotationSymbol property.");
}

tests.testEndpointHasGenesWithAnnotationSymbol = testEndpointHasGenesWithAnnotationSymbol;

function testEndpointHasNoGenesWithoutAnnotationSymbol (testcase) {
	var query = "ASK { ?gene rdf:type flybase:Gene . OPTIONAL {?gene flybase:annotationSymbol ?annotationSymbol } . FILTER ( !bound(?annotationSymbol)) }";
	testAskQuery("flybase", testcase, query, false, "Expected no genes without annotationSymbol property.");	
}

tests.testEndpointHasNoGenesWithoutAnnotationSymbol = testEndpointHasNoGenesWithoutAnnotationSymbol;

function testEndpointHasNoGenesWithMultipleAnnotationSymbol (testcase) {
	var query = "ASK { ?gene flybase:annotationSymbol ?annotationSymbol1, ?annotationSymbol2 . FILTER ( ?annotationSymbol1 != ?annotationSymbol2 ) }";
	testAskQuery("flybase", testcase, query, false, "Expected no genes with multiple annotationSymbol properties.");	
}

tests.testEndpointHasNoGenesWithMultipleAnnotationSymbol = testEndpointHasNoGenesWithMultipleAnnotationSymbol;

function testEndpointHasGenesWithFullName (testcase) {
	var query = "ASK { ?gene flybase:fullname ?fullname }";
	testAskQuery("flybase", testcase, query, true, "Endpoint has no genes with fullname property.");
}

tests.testEndpointHasGenesWithFullName = testEndpointHasGenesWithFullName;

function testEndpointHasNoGenesWithoutFullName (testcase) {
	var query = "ASK { ?gene rdf:type flybase:Gene . OPTIONAL {?gene flybase:fullname ?fullname }. FILTER ( !bound(?fullname)) }";
	testAskQuery("flybase", testcase, query, false, "Expected no genes without fullname property.");	
}

tests.testEndpointHasNoGenesWithoutFullName = testEndpointHasNoGenesWithoutFullName;

function testEndpointHasNoGenesWithMultipleFullName (testcase) {
	var query = "ASK { ?gene flybase:fullname ?fullname1, ?fullname2 . FILTER ( ?fullname1 != ?fullname2 ) }";
	testAskQuery("flybase", testcase, query, false, "Expected no genes with multiple fullname properties.");	
}

tests.testEndpointHasNoGenesWithMultipleFullName = testEndpointHasNoGenesWithMultipleFullName;

function testEndpointHasGenesWithSynonym (testcase) {
	var query = "ASK { ?gene flybase:synonym ?synonym }";
	testAskQuery("flybase", testcase, query, true, "Endpoint has no genes with synonym property.");
}

tests.testEndpointHasGenesWithSynonym = testEndpointHasGenesWithSynonym;

function testEndpointHasNoGenesWithoutSynonym (testcase) {
	var query = "ASK { ?gene rdf:type flybase:Gene . OPTIONAL {?gene flybase:synonym ?synonym }. FILTER ( !bound(?synonym)) }";
	testAskQuery("flybase", testcase, query, false, "Expected no genes without synonym property.");	
}

tests.testEndpointHasNoGenesWithoutSynonym = testEndpointHasNoGenesWithoutSynonym;

function testEndpointHasGenesWithAnyName (testcase) {
	var query = "ASK { ?gene flybase:anyname ?anyname }";
	testAskQuery("flybase", testcase, query, true, "Endpoint has no genes with anyname property.");
}

tests.testEndpointHasGenesWithAnyName = testEndpointHasGenesWithAnyName;

function testEndpointHasNoGenesWithoutAnyName (testcase) {
	var query = "ASK { ?gene rdf:type flybase:Gene . OPTIONAL {?gene flybase:anyname ?anyname }. FILTER ( !bound(?anyname)) }";
	testAskQuery("flybase", testcase, query, false, "Expected no genes without anyname property.");	
}

tests.testEndpointHasNoGenesWithoutAnyName = testEndpointHasNoGenesWithoutAnyName;

var flybaseTestCase = new YAHOO.tool.TestCase({

	name: "FlyBase gene names SPARQL Endpoint Test Case",
	
	testEndpointExists : function() {
		testEndpointExists(this);
	},
	
	testEndpointHasGenes : function() {
		testEndpointHasGenes (this);
	},
	
	testEndpointHasGenesWithFlyBaseId : function() {
		testEndpointHasGenesWithFlyBaseId (this); 
	},

	
	testEndpointHasNoGenesWithoutFlyBaseId : function() {
		testEndpointHasNoGenesWithoutFlyBaseId (this);
	},
	

	testEndpointHasNoGenesWithMultipleFlyBaseId : function() {
		testEndpointHasNoGenesWithMultipleFlyBaseId (this);
	},
	
	testEndpointHasGenesWithSymbol : function() {
		testEndpointHasGenesWithSymbol (this); 
	},

    /* This test fails: 
     * two entries are returned without symbols, but don't seem to correspond
     * actual Flybase genes:  CR41445 and CR33987.
	testEndpointHasNoGenesWithoutSymbol : function() {
		testEndpointHasNoGenesWithoutSymbol (this);
	},
     */ 	

    testEndpointHasNoGenesWithMultipleSymbol : function() {
		testEndpointHasNoGenesWithMultipleSymbol (this);
	},
    
	
	testEndpointHasGenesWithAnnotationSymbol : function() {
		testEndpointHasGenesWithAnnotationSymbol (this); 
	},
	
	
	/* This test did not pass ... 
	 * several genes without annotation symbols
	testEndpointHasNoGenesWithoutAnnotationSymbol : function() {
		testEndpointHasNoGenesWithoutAnnotationSymbol (this);
	},*/

	
	/* This test did not pass ...
	 * only one annotation symbol is current
	 testEndpointHasNoGenesWithMultipleAnnotationSymbol : function() {
		testEndpointHasNoGenesWithMultipleAnnotationSymbol (this);
	},*/
	
	testEndpointHasGenesWithFullName : function() {
		testEndpointHasGenesWithFullName (this); 
	},

	/* This test failed
	 * lots of entries for alleles, and a few genes without full names
	testEndpointHasNoGenesWithoutFullName : function() {
		testEndpointHasNoGenesWithoutFullName (this);
	},
	*/

	/* This test failed
	 * only one is current
	testEndpointHasNoGenesWithMultipleFullName : function() {
		testEndpointHasNoGenesWithMultipleFullName (this);
	},
	*/
	
    testEndpointHasGenesWithSynonym : function() {
		testEndpointHasGenesWithSynonym (this); 
	},

	/* This test failed
	 * two entries are returned without synonyms, but don't seem to correspond
     * actual Flybase genes:  CR41445 and CR33987.
     * the same pair that does not have symbols
	testEndpointHasNoGenesWithoutSynonym : function() {
		testEndpointHasNoGenesWithoutSynonym (this);
	},
	*/

	testEndpointHasGenesWithAnyName : function() {
		testEndpointHasGenesWithAnyName (this); 
	},

	testEndpointHasNoGenesWithoutAnyName : function() {
		testEndpointHasNoGenesWithoutAnyName (this);
	},

})

function setupLogger() {
	var logReader = new YAHOO.tool.TestLogger("logger");
	YAHOO.log("Logger setup done");	
}

function runTests() {
	YAHOO.log("Running tests");
	YAHOO.tool.TestRunner.clear();
	YAHOO.tool.TestRunner.add(flybaseTestCase);
	YAHOO.tool.TestRunner.run();
}

function runTest(testName) {
	var fn = tests[testName];
	if (typeof fn != "undefined" || fn) {
		var tfn = function() {fn(this)};
		var to = {
			name : testName
		};
		to[testName] = tfn;
		var testCase = new YAHOO.tool.TestCase(to);
		YAHOO.tool.TestRunner.clear();
		YAHOO.tool.TestRunner.add(testCase);
		YAHOO.tool.TestRunner.run();
	}
}

YAHOO.util.Event.onDOMReady(setupLogger);
//YAHOO.util.Event.onDOMReady(runTests);

